package com.jcark.mvvm.net

import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import com.jcark.arch.net.NetConfigManager
import com.jcark.arch.net.state.NetWorkMonitorManager
import com.jcark.arch.net.state.NetWorkState
import com.jcark.arch.net.state.NetworkCallback
import com.jcark.arch.net.utils.AESOperator
import com.jcark.mvvm.R
import me.hgj.mvvm.ext.util.TAG
import me.hgj.mvvm.state.ResultState
import java.io.File

class NetTestActivity : AppCompatActivity() {
    val loginRegisterViewModel by viewModels<RequestLoginRegisterViewModel>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_net_test)
        findViewById<Button>(R.id.btn_test_login).setOnClickListener {
//            loginRegisterViewModel.loginReq(mobile = "18943690992", password = "1234567890")
            loginRegisterViewModel.loginReq()
        }
        Log.d(TAG,"1234567890")
        loginRegisterViewModel.encryptionResult.observe(this, Observer { resultState ->
            if (resultState is ResultState.Success) {
                resultState.data?.let {
                    NetConfigManager.INSTANCE.setSign(it.sign)
                    var key = it.key;
                    var decrypt = AESOperator.getInstance().decrypt(key)
                    val serverIp: String = AESOperator.getInstance()
                        .decryptBySkey(it.serverIP, decrypt) //解密后的serverIp
                    loginRegisterViewModel.loginHost = serverIp
                    NetConfigManager.INSTANCE.changeBaseUrl("$serverIp/")
                    Log.d("JCArk", "serverIp   $serverIp")
                }

            }
        })

        findViewById<Button>(R.id.btn_test_login2).setOnClickListener {
            loginRegisterViewModel.loginReq(mobile = "15165321111", password = "111111")
//            loginRegisterViewModel.loginReq(mobile = "17860743518", password = "asdf1234")
        }
        loginRegisterViewModel.loginResult.observe(this, Observer {
            if (it is ResultState.Success) {
                it.data?.let {
                    NetConfigManager.INSTANCE.setToken(it.token)
                }

            }
        })

        NetWorkMonitorManager.register(object : NetworkCallback {
            override fun onNetChange(netWorkState: NetWorkState) {
                Log.d("JCArk", "网络发生变化${netWorkState.name}")
            }
        })
        findViewById<Button>(R.id.btn_test_login3).setOnClickListener {
//            /sdcard/Simulator Screen Shot - iPhone 13 Pro Max - 2022-05-17 at 10.11.45.png
            loginRegisterViewModel.upImg(File("/sdcard/11111.png"))
        }
        findViewById<Button>(R.id.btn_test_login4).setOnClickListener {
//            /sdcard/Simulator Screen Shot - iPhone 13 Pro Max - 2022-05-17 at 10.11.45.png
            NetConfigManager.INSTANCE.changeBaseUrl("http://10.10.2.60:8009/")
        }
        findViewById<Button>(R.id.btn_test_login5).setOnClickListener {
//            /sdcard/Simulator Screen Shot - iPhone 13 Pro Max - 2022-05-17 at 10.11.45.png
            loginRegisterViewModel.sendCode("18943690992")
        }


    }
}

//      {
//          "APP_ID": "yapp-app-android-001",
//          "data": "\/YLAIHGZCK19UGtJx7bWurCGnV96aMIEKH7QZqK+uaM=",
//          "netStatus": "WiFi",
//          "platform": 2,
//          "randomKey": "W57d9sSuR1QqAH1xWAB8hbupBs0zHrNJN2EDzsrEIG4=",
//          "safeCode": "27ff962ac55910f067afad60ce1d51db",
//          "sign": "a0c4a33c-5d03-4e9b-a478-70bebdc6925b2bwt938",
//          "timestamp": "1653968174997",
//          "token": "",
//          "version": "1.0.0"
//      }